package sgi.client;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

import sgi.details_new.SubscribeDetails;

public class SGIReviewSubscriptionsUI extends SGIClientFrame {

	private JPanel contentPane;
	private JScrollPane scrollPane;
	private JTable table;
	private static SGIReviewSubscriptionsController ReviewController;
	DefaultTableModel dtm;
	/** constructor */
	public SGIReviewSubscriptionsUI() 
	{
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 659, 525);		
		
		setTitle("Pending subscriptions table");
		contentPane = new JPanel();
		getContentPane().setLayout(null);
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		/** button to return to useraccountmenu */
		JButton btnReturn = new JButton("Return");
		btnReturn.setBounds(10, 450, 123, 36);
		getContentPane().add(btnReturn);
		btnReturn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SGIReviewSubscriptionsController.terminate();
				SGIUserAccountMenuController.create(null);
			}
		});
			
		/** setting table of types of subscriptions */
		Object[] columnNames = {"UserName","Type"};
		Object rowData[][]={};
		DefaultTableModel dtm = new DefaultTableModel(rowData, columnNames);
		table = new JTable(dtm);
		table.setBounds(10, 127, 633, 321);
		
		JTableHeader header = table.getTableHeader();
		
		JPanel panel = new JPanel();
		panel.setBounds(10, 107, 633, 332);
		panel.setLayout(new BorderLayout());
		panel.add(header, BorderLayout.NORTH);
		panel.add(table, BorderLayout.CENTER);

		getContentPane().add(panel);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		panel.add(table);
		
		/** sending that accepted subscriptions of customer from types */
		JButton btnAccept = new JButton("Accept");
		btnAccept.setBounds(520, 450, 123, 36);
		btnAccept.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int sel = table.getSelectedRow();
				String userName=(String) table.getValueAt(sel, 0);
				String Type=(String) table.getValueAt(sel, 1);
				/** checking the type and sending */
				if(Type.equals("DataLayer"))
					ReviewController.AcceptSubscriptionsDataLayer(userName);
				else if(Type.equals("DateTime"))
					ReviewController.AcceptSubscriptionsDateTime(userName);
				else
					ReviewController.AcceptSubscriptionsImagePack(userName);
				
				
				review();
			}
		});
		getContentPane().add(btnAccept);
		

		
		/** setting size of table */
		TableColumn column=null;
		for(int i=0;i<table.getColumnCount(); i++)
		{
			column= table.getColumnModel().getColumn(i);
			if(i== 3)
			{

				column.setWidth(200);
			}
			if(i== 4)
			{

				column.setWidth(600);
			}
		}
	}

	/** set controller */
	public void setReviewSubscriptionsController(SGIReviewSubscriptionsController review)
	{
		SGIReviewSubscriptionsUI.ReviewController = review;
	} 
	/** add rows to table */
	public void addRows(SubscribeDetails cmd)
	{
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		dtm.insertRow(table.getRowCount(), new Object[] {cmd.getUserName(),cmd.getType()});
		table.setModel(dtm);
	}
	/** clear table */
	public void clearTable()
	{
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		while(dtm.getRowCount() > 0) {dtm.removeRow(0);}
	}	
	/** review UI */
	public void review()
	{
		SGIReviewSubscriptionsController.subscriptionsReviewUI.clearTable();
		ReviewController.ReviewSubscriptionsDataLayer();	
		ReviewController.ReviewSubscriptionsDateTime();
		ReviewController.ReviewSubscriptionsImagePack();
	}

}
